package com.timesheet.gui.efforttracker.editor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.timesheet.gui.efforttracker.composite.WSRDateChooseComposite;
import com.timesheet.gui.efforttracker.container.UtilisationModelContainer;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.session.Session;
import com.timesheet.serverice.UtlisationService;

public class WSRDateChooseController {

	UtilisationModelContainer utilisationModelContainer = UtilisationModelContainer
			.getInstance();

	public WSRDateChooseController(WSRDateChooseComposite wsrDateChooseComposite) {
		performActions(wsrDateChooseComposite);
	}

	void performActions(WSRDateChooseComposite wsrDateChooseComposite) {
		wsrDateChooseComposite.getBtnSubmit().addSelectionListener(
				new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						save();
					}

				});
	}

	private void save() {
		System.out.println("saved");
		List<EffortTrackerDTO> utilisationList = new UtlisationService()
				.geUtilisation(Session.INSTANCE.getProjects());
		utilisationModelContainer.setUtilisationList(utilisationList);

		//List<Double> convertToOneRowFormat = convertToOneRowFormat(utilisationList);
		Map<String, EffortTrackerDTO> convertToOneRowFormatMap = convertToOneRowFormatMap(utilisationList);
		utilisationModelContainer.setUtilisationMap(convertToOneRowFormatMap);
		//utilisationModelContainer.setHoursList(convertToOneRowFormat);
		utilisationModelContainer.refresh();

	}



	private Map<String, EffortTrackerDTO> convertToOneRowFormatMap(
			List<EffortTrackerDTO> utilisationList) {	
		Map<String, EffortTrackerDTO> dtoMap = new HashMap<String, EffortTrackerDTO>();
		for (EffortTrackerDTO dto : utilisationList) {
			if (dto.getProjectId().equals("ABSLIFE")) {				
				dtoMap.put("ABSLIFE", dto);
				
			} else if (dto.getProjectId().equals("ABSKRAFT")) {
				dtoMap.put("ABSKRAFT", dto);
			}
			if (dto.getProjectId().equals("ABSCFS")) {
				dtoMap.put("ABSCFS", dto);
			} else if (dto.getProjectId().equals("ABSHOST")) {
				dtoMap.put("ABSHOST", dto);
			}
		}

		return dtoMap;

	}

}
